package com.test.deencredit.bean_deencredit.process_deencredit;

import static com.test.deencredit.config_deencredit.UserState_deencredit.State_Fail_deencredit;
import static com.test.deencredit.config_deencredit.UserState_deencredit.State_NoOrder_deencredit;
import static com.test.deencredit.config_deencredit.UserState_deencredit.State_Rp_deencredit;
import static com.test.deencredit.config_deencredit.UserState_deencredit.State_Wait_deencredit;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.test.deencredit.bean_deencredit.config_deencredit.AppShowCustomize_deencredit;
import com.test.deencredit.status_deencredit.ApplyStatus_deencredit;
import com.test.deencredit.status_deencredit.OrderStatus_deencredit;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class ProcessInfo_deencredit {

    private static class Inner_deencredit {
        private static ProcessInfo_deencredit instance = new ProcessInfo_deencredit();
    }

    private ProcessInfo_deencredit() {}

    public static ProcessInfo_deencredit getInstance_deencredit() {
        return Inner_deencredit.instance;
    }

    public static void parse_deencredit(JSONObject jsonObject) {
        String jsonStr = jsonObject.toString();
        Gson gson = new Gson();
        Inner_deencredit.instance = gson.fromJson(jsonStr, ProcessInfo_deencredit.class);
        try {
            Inner_deencredit.instance.srcJSON = new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //判断当前订单状态显示对应的界面
        if (Inner_deencredit.instance.isOrderPendingRepayment_deencredit()) {//是否在待还款-未逾期
            Inner_deencredit.instance.setLoanState_deencredit(LoanState_deencredit.Order_Pending_Repayment);
        } else if (Inner_deencredit.instance.isOrderOverdueRepayment_deencredit()) {//是否在待还款-已逾期
            Inner_deencredit.instance.setLoanState_deencredit(LoanState_deencredit.Order_Overdue_Repayment);
        } else if (Inner_deencredit.instance.isOrderLoaning_deencredit()) {//是否在待放款
            Inner_deencredit.instance.setLoanState_deencredit(LoanState_deencredit.Order_Loaning);
        } else if (Inner_deencredit.instance.isLoanRejcted_deencredit()) {//申请是否被拒绝
            Inner_deencredit.instance.setLoanState_deencredit(LoanState_deencredit.Loan_Rejected);
        } else if (Inner_deencredit.instance.isLoanReviewing_deencredit()) {//是否有审核中的贷款
            Inner_deencredit.instance.setLoanState_deencredit(LoanState_deencredit.Loan_Reviewing);
        } else {
            Inner_deencredit.instance.setLoanState_deencredit(LoanState_deencredit.No_Order);
        }
    }

    private String product;
    private ArrayList<String> allStepTitles;
    private ArrayList<String> allSteps;
    private ArrayList<String> steps;
    private LoanState_deencredit loanState;
    private String idCard;
    private String idCardImg;
    private String birthDate;
    private String applyStatus;//申请订单状态
    private String orderStatus;//还款订单状态
    private String deniedBetweenDay;//被拒重新申请天数
    private String userType;
    private JSONObject srcJSON;

    private String recipientName;

    public String getProduct_deencredit() {
        if (TextUtils.isEmpty(product)) return "";
        return product;
    }

    public int getStepsSize_deencredit(){
        if (steps==null) return 0;
        return steps.size();
    }

    public String getIdCard_deencredit() {
        if (TextUtils.isEmpty(idCard)) return "";
        return idCard;
    }

    public String getIdCardImg_deencredit() {
        if (TextUtils.isEmpty(idCardImg)) return "";
        return idCardImg;
    }

    public String getStepName_deencredit(int index){
        if (steps==null||index<0||index>=steps.size()) return "";
        return steps.get(index);
    }

    public String getH5Url_deencredit(){
        if (srcJSON==null) return "";
        return srcJSON.optString("h5");
    }

    public ProcessInfo_Step_deencredit getStep_deencredit(int index){
        try {
            if (steps==null||index<0||index>=steps.size()||srcJSON==null) return null;
            String stepName = steps.get(index);

            String stepStr = "";

            if (srcJSON.has(stepName+"_desc")) stepStr = srcJSON.optString(stepName+"_desc");
            else stepStr = srcJSON.optString(stepName);


            if (TextUtils.isEmpty(stepStr) || stepStr.equalsIgnoreCase("null") || !stepStr.startsWith("{"))
                stepStr = AppShowCustomize_deencredit.getInstance_deencredit().getOther_deencredit(stepName+"_desc");

            if (TextUtils.isEmpty(stepStr)) return null;

            return new Gson().fromJson(stepStr, ProcessInfo_Step_deencredit.class);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    public ArrayList<String> getSteps_deencredit() {
        return steps;
    }

    public int getPercent_deencredit(){
        if (steps==null||allSteps==null||steps.isEmpty()||allSteps.isEmpty()) return 0;
        int finishStepSize = allSteps.size()-steps.size();
        if (finishStepSize<0) return 100;
        int result = (int) Math.round(((double)finishStepSize)/((double)allSteps.size())*100);
        if (result == 100) result--;
        return result;
    }

    public void setLoanState_deencredit(LoanState_deencredit loanState) {
        this.loanState = loanState;
    }

    public String getOrderStatus_deencredit() {
        if (TextUtils.isEmpty(orderStatus)) return "";
        return orderStatus;
    }

    public String getApplyStatus_deencredit() {
        if (TextUtils.isEmpty(applyStatus)) return "";
        return applyStatus;
    }

    public String getDeniedBetweenDay_deencredit() {
        if (TextUtils.isEmpty(deniedBetweenDay)) return "30";
        return deniedBetweenDay;
    }

    public ArrayList<String> getAllSteps_deencredit() {
        if (allSteps==null) return new ArrayList<>();
        return allSteps;
    }

    public String getRecipientName_deencredit() {
        if (TextUtils.isEmpty(recipientName)) return "";
        return recipientName;
    }

    public boolean isNewUser_deencredit(){
        if (TextUtils.isEmpty(recipientName)) return true;
        return !userType.equalsIgnoreCase("OLD");
    }

    public String getStepTitle_deencredit(int index){
        if (allStepTitles==null || index<0 || index>=allStepTitles.size()) return "";
        return allStepTitles.get(index);
    }

    public String getProcessNum_deencredit(){
        if (allSteps==null||steps==null||allSteps.isEmpty()) return "0";
        int result=0;
        result = allSteps.size()-steps.size()+1;
        return String.valueOf(Math.max(result, 0));
    }

    /**
     * 待放款
     */
    public boolean isOrderLoaning_deencredit() {
        return (getOrderStatus_deencredit().equalsIgnoreCase(OrderStatus_deencredit.CREATED_deencredit) ||
                getOrderStatus_deencredit().equalsIgnoreCase(OrderStatus_deencredit.LENDING_deencredit));
    }

    /**
     * 待还款-未逾期
     */
    public boolean isOrderPendingRepayment_deencredit() {
        return getOrderStatus_deencredit().equalsIgnoreCase(OrderStatus_deencredit.LOANED_deencredit);
    }

    /**
     * 待还款-已逾期
     */
    public boolean isOrderOverdueRepayment_deencredit() {
        return (getOrderStatus_deencredit().equalsIgnoreCase(OrderStatus_deencredit.OVERDUE_deencredit) ||
                getOrderStatus_deencredit().equalsIgnoreCase(OrderStatus_deencredit.BAD_DEBT_deencredit));
    }

    /**
     * 申请拒绝
     */
    public boolean isLoanRejcted_deencredit() {
        return (getOrderStatus_deencredit().equalsIgnoreCase(OrderStatus_deencredit.DENIED_deencredit) ||
                getApplyStatus_deencredit().equalsIgnoreCase(ApplyStatus_deencredit.SYS_DENIED_deencredit) ||
                getApplyStatus_deencredit().equalsIgnoreCase(ApplyStatus_deencredit.MANU_DENIED_deencredit) ||
                getApplyStatus_deencredit().equalsIgnoreCase(ApplyStatus_deencredit.DS_DENIED_deencredit));
    }

    /**
     * 申请审核中
     */
    public boolean isLoanReviewing_deencredit() {
        return (getApplyStatus_deencredit().equalsIgnoreCase(ApplyStatus_deencredit.CREATED_deencredit) ||
                getApplyStatus_deencredit().equalsIgnoreCase(ApplyStatus_deencredit.SYS_CHECK_deencredit) ||
                getApplyStatus_deencredit().equalsIgnoreCase(ApplyStatus_deencredit.SYS_APROVAL_deencredit) ||
                getApplyStatus_deencredit().equalsIgnoreCase(ApplyStatus_deencredit.MANU_APROVAL_deencredit) ||
                getApplyStatus_deencredit().equalsIgnoreCase(ApplyStatus_deencredit.DS_APROVAL_deencredit));
    }

    public String getUserState_deencredit(){
        if (isOrderPendingRepayment_deencredit() || isOrderOverdueRepayment_deencredit()) //订单状态-待还款
            return State_Rp_deencredit;
        else if (isOrderLoaning_deencredit() || isLoanReviewing_deencredit()) //订单状态-放款中/审核中
            return State_Wait_deencredit;
        else if (isLoanRejcted_deencredit()) //订单状态-被拒绝
            return State_Fail_deencredit;
        else return State_NoOrder_deencredit;
    }

    public enum LoanState_deencredit {
        ///////////////////
        No_Order,//没有订单
        Loan_Rejected,//申请拒绝
        Loan_Reviewing,//申请审核中
        Order_Loaning,//待放款
        Order_Pending_Repayment,//待还款，未逾期
        Order_Overdue_Repayment,//待还款,已逾期
    }
}
